package cn.tomas.queue.delayQueue.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 延迟队列配置
 * @author Mr.Still
 * @version 1.0.0
 * @date 2021-11-11 16:45
 **/
@Configuration
public class DelayQueueConfig {

    /**
     * 延迟交换机
     */
    public static final String DELAY_EXCHANGE_NAME = "delay.queue.demo.business.exchange";
    /**
     * 延迟队列A B
     */
    public static final String DELAY_QUEUE_A_NAME = "delay.queue.demo.business.queuea";
    public static final String DELAY_QUEUE_B_NAME = "delay.queue.demo.business.queueb";
    /**
     * 延迟路由key A B
     */
    public static final String DELAY_QUEUE_A_ROUTING_KEY = "delay.queue.demo.business.queuea.routingkey";
    public static final String DELAY_QUEUE_B_ROUTING_KEY = "delay.queue.demo.business.queueb.routingkey";
    /**
     * 死信交换机
     */
    public static final String DEAD_LETTER_EXCHANGE = "delay.queue.demo.deadletter.exchange";
    /**
     * 死信路由key A B
     */
    public static final String DEAD_LETTER_QUEUE_A_ROUTING_KEY = "delay.queue.demo.deadletter.delay_10s.routingkey";
    public static final String DEAD_LETTER_QUEUE_B_ROUTING_KEY = "delay.queue.demo.deadletter.delay_60s.routingkey";
    /**
     * 死信队列 A B
     */
    public static final String DEAD_LETTER_QUEUE_A_NAME = "delay.queue.demo.deadletter.queuea";
    public static final String DEAD_LETTER_QUEUE_B_NAME = "delay.queue.demo.deadletter.queueb";


    /**
     * 延迟队列 交换机
     */
    @Bean("delayExchange")
    public DirectExchange delayExchange(){
        return new DirectExchange(DELAY_EXCHANGE_NAME);
    }

    /**
     * 死信队列 交换机
     */
    @Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange(){
        return new DirectExchange(DEAD_LETTER_EXCHANGE);
    }

    /**
     * 延迟队列A 延迟时间10s
     */
    @Bean("delayQueueA")
    public Queue delayQueueA(){
        Map<String,Object> params = new HashMap<String,Object>();
        //x-dead-letter-exchange  声明当前队列绑定的死信交换机
        params.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE);
        //x-dead-letter-routing-key   声明当前队列的死信路由key
        params.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUE_A_ROUTING_KEY);
        //x-message-ttl   声明队列的TTL时间（Time To Live）
        params.put("x-message-ttl",6000);
        return QueueBuilder.durable(DELAY_QUEUE_A_NAME).withArguments(params).build();
    }

    /**
     * 延迟队列B  延时 50s
     */
    @Bean("delayQueueB")
    public Queue delayQueueB(){
        Map<String,Object> params = new HashMap<String,Object>();
        //x-dead-letter-exchange  声明当前队列绑定的死信交换机
        params.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE);
        //x-dead-letter-routing-key   声明当前队列的死信路由key
        params.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUE_B_ROUTING_KEY);
        //x-message-ttl   声明队列的TTL时间（Time To Live）
        params.put("x-message-ttl",50000);
        return QueueBuilder.durable(DELAY_QUEUE_B_NAME).withArguments(params).build();
    }

    /**
     * 声明死信队列A  用于接收延时10s处理的消息
     */
    @Bean("deadLetterQueueA")
    public Queue deadLetterQueueA(){
        return new Queue(DEAD_LETTER_QUEUE_A_NAME);
    }

    /**
     * 声明死信队列B  用于接收延时60s处理的消息
     */
    @Bean("deadLetterQueueB")
    public Queue deadLetterQueueB(){
        return new Queue(DEAD_LETTER_QUEUE_B_NAME);
    }

    /**
     *  延迟交换机 绑定 延迟队列A
     */
    @Bean
    public Binding delayBindingA(@Qualifier("delayQueueA")Queue queue,@Qualifier("delayExchange")DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(DELAY_QUEUE_A_ROUTING_KEY);
    }

    /**
     *  延迟交换机 绑定 延迟队列B
     */
    @Bean
    public Binding delayBindingB(@Qualifier("delayQueueB")Queue queue,@Qualifier("delayExchange")DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(DELAY_QUEUE_B_ROUTING_KEY);
    }

    /**
     *  死信交换机 绑定 死信队列A
     */
    @Bean
    public Binding deadBindingA(@Qualifier("deadLetterQueueA")Queue queue,@Qualifier("deadLetterExchange")Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_QUEUE_A_ROUTING_KEY).noargs();
    }

    /**
     *  死信交换机 绑定 死信队列B
     */
    @Bean
    public Binding deadBindingB(@Qualifier("deadLetterQueueB")Queue queue,@Qualifier("deadLetterExchange")Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_QUEUE_B_ROUTING_KEY).noargs();
    }
}
